package com.example.big_orange2.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.big_orange2.authorization.annotation.AuthIgnore;
import com.example.big_orange2.common.Result;

import com.example.big_orange2.entity.pagedata_m;
import com.example.big_orange2.entity.sharelock;
import com.example.big_orange2.mapper.sharelockMapper;
import com.example.big_orange2.service.ShareLockService;
import javafx.util.Pair;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/sharelock")
public class sharelockController {
    @Resource
    sharelockMapper sharelockMapper;
    @Resource
    ShareLockService shareLockService;
    //增
    //向数据库写入（insert）数据的接口
    @PostMapping("/add")
    public Result<?> save(@RequestBody sharelock sharelock){//?是泛型，表示任意类型
        //设置默认值
        //插入的操作
        sharelockMapper.insert(sharelock);
        return Result.success();//返回操作情况
    }
    //向数据库删除（delete）数据的接口
    @DeleteMapping("/delete/{sharelockid}")//占位
    public Result<?> delete(@PathVariable String sharelockid){//?是泛型，表示任意类型
        //删除的操作
        sharelockMapper.deleteById(sharelockid);
        return Result.success();//返回操作情况

    }
    //修改功能
    @PutMapping("/update")
    //向数据库更新（update）数据的接口
    public Result<?> update(@RequestBody sharelock sharelock){//?是泛型，表示任意类型
        sharelockMapper.updateById(sharelock);
        return Result.success();
    }

    //全部查询
    @GetMapping("/getdata")
    //根据员工号，挑选他负责的客户信息
    public Result<?> finduser(){
        LambdaQueryWrapper<sharelock> wrapper = Wrappers.<sharelock>lambdaQuery();
//        wrapper.orderByAsc(sharelock::getStaffid);//排序
//        QueryWrapper<sharelock> qw = new QueryWrapper<>();
//        qw.groupBy("staffid");
//        wrapper.groupBy(sharelock::getStaffid);//分组
        List<sharelock> Loanhandle = sharelockMapper.selectList(wrapper);//
        return  Result.success(Loanhandle);//返回结果

    }
    @GetMapping("/getdatass")
    public Result<?> findbysum(){
        Result<?> result=new Result<>();
        result=shareLockService.findbysum();
        return  result;
    }


    //依据序号
    @GetMapping("/getdataseq/{sharelockid}")
    public Result<?> findbyseq(@PathVariable String sharelockid){
        LambdaQueryWrapper<sharelock> wrapper =Wrappers.<sharelock>lambdaQuery();
        wrapper.eq(sharelock::getSharelockid,sharelockid);//多条件查询
        List<sharelock> Loanhandle = sharelockMapper.selectList(wrapper);//
        return  Result.success(Loanhandle);//返回结果

    }
    //依据员工号
    @GetMapping("/getdatauser/{staffid}")
    public Result<?> findbyuser(@PathVariable String staffid){
        LambdaQueryWrapper<sharelock> wrapper =Wrappers.<sharelock>lambdaQuery();
        wrapper.eq(sharelock::getStaffid,staffid);//多条件查询
        List<sharelock> Loanhandle = sharelockMapper.selectList(wrapper);//
        return  Result.success(Loanhandle);//返回结果

    }
    //依据员工号
    @GetMapping("/getdatauser/{staffid}/{position}")
    public Result<?> findbyuserweb(@PathVariable String staffid,@PathVariable Integer position){
        LambdaQueryWrapper<sharelock> wrapper =Wrappers.<sharelock>lambdaQuery();
        if(position==1){
            List<sharelock> Loanhandle = sharelockMapper.selectList(wrapper);//
            return  Result.success(Loanhandle);//返回结果
        }else{
            wrapper.eq(sharelock::getStaffid,staffid);//多条件查询
            List<sharelock> Loanhandle = sharelockMapper.selectList(wrapper);//
            return  Result.success(Loanhandle);//返回结果
        }


    }
    @PostMapping("/paging/{page}/{limit}")
    //分页查询(select),查询数据接口
    public Result<?> findPage(@PathVariable Integer page,
                              @PathVariable Integer limit,
                              @RequestBody(required = false)  String s
                              ){
        Result<?> result=new Result<>();
        result=shareLockService.findPage(page,limit,s);
        return  result;
    }
}
